package com.jmb.api.web.controller.temp;

import com.jmb.api.common.result.Result;
import com.jmb.api.web.service.TempPostProcessorValueService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "临时变量")
@RequestMapping("/web/tempPostProcessorValue")
@RestController
public class TempPostProcessorValueController {
    @Autowired
    private TempPostProcessorValueService tempPostProcessorValueService;
    // 获取所有临时变量
    @GetMapping("/env")
    public Result findAllTempValue(@RequestParam HashMap<String,String> query){
        ArrayList<Object> list = new ArrayList<>();
        String key = query.get("key");
        Map<Object,Object> map;
        if (key == null || key.isEmpty()){
            map = tempPostProcessorValueService.findAllTempValue();
        }else {
            map = (tempPostProcessorValueService.findTempValueByValue(key));
        }
        for (Map.Entry<Object, Object> entry: map.entrySet()){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("key",entry.getKey());
            hashMap.put("value",entry.getValue());
            list.add(hashMap);
        }
        return Result.ok(list);

    }

    /**
     * 移除临时后置处理器值
     * @param key  key
     * @return  Result
     */
    @GetMapping("/env/remove/{key}")
    public Result removeTempValue(String key){
        tempPostProcessorValueService.removeTempValue(key);
        return Result.ok();
    }

    /**
     * 移除全部临时后置处理器值
     *
     * @return Result
     */
    @GetMapping("/env/removeAll")
    public Result removeAllTempValue() {
        tempPostProcessorValueService.removeAllTempValue();
        return Result.ok("删除成功");
    }

}
